<?php
//This controller handles the files that can be uploaded to the community.
//STILL UNDER CONSTRUCTION.

session_start();

/**
 * Description of files
 *
 * @author bilde
 */
class files extends CI_Controller {


	function  __construct()
        {
           parent::__construct();

           //Logged_in control
	   if (!$this->session->userdata('is_logged_in'))
           {
                redirect('/welcome/index', 'refresh');
	   }

	}

        //Load the files-view and list all files from the files-table.
	function index(){
		$data['title'] = 'List of Files';
		$data['main_content'] = 'file/files_home';
		$data['user'] = $this->model_users->getUserByUsername($this->session->userdata('id'));
		$data['results'] = $this->model_files->list_files_short();
//		$data['file_tags'] = $this->model_tags->list_tag_objects("files");
//		$data['comments'] = $this->model_comments->list_comments(array_keys($data['results']),'files');
		$data['usernames'] = $this->model_users->getAllUserNames();
		$this->load->vars($data);
		$this->load->view('includes/template');
	}

        //Load the file-view for tagget file($id)
	function view_file($id){
		$file = $this->model_files->get_file($id);
		$data['title'] = $file->title;
		$data['main_content'] = 'file/view_file';
		$data['user'] = $this->model_users->getUserByUsername($this->session->userdata('id'));
		$data['results'] = $file;
		$data['usernames'] = $this->model_users->getAllUserNames();
		//$data['file_tags'] = $this->model_tags->list_tag_objects_single($id,'files');
		//$data['comments'] = $this->model_comments->list_comments_single($id,'files');
		$this->load->vars($data);
		$this->load->view('includes/template');
	}

        //Download tagget file($id)
        //Still under development.
	function download($id)
        {
            $this->model_files->get_data($id);
	}

        //Uploads a file.
	function upload(){
		$try = $this->model_files->add_file();


		if ($try == '0'){
			echo "No file uploaded!";
			exit();
		}else{
			$data['file_id'] = $try;
		}

		$data['title'] = 'Add File Details';
		$data['main_content'] = 'file/add_file';
		$data['user'] = $this->model_users->getUserByUsername($this->session->userdata('id'));
		$this->load->vars($data);
		$this->load->view('includes/template');

	}

        //Shall add deails to file.
        //Still under development.
	function add_details(){
		$try = $this->model_files->add_details();
		redirect("files/index", 'refresh');
	}

}
?>
